package com.qf.myshop.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * 基本的Servlet
 * 用来将同一个业务的Servlet功能进行集中处理
 */
public class BaseServlet extends HttpServlet{
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8"); // 转码
        // 默认业务方法的获取
        String m = request.getParameter("m");
        if (m == null || m.equals("")){
            m = "list";
        }
        // 通过反射的方式调用业务方法
        try {
            this.getClass().getMethod(m, HttpServletRequest.class, HttpServletResponse.class)
                    .invoke(this, request, response);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
